import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {PagedData} from '../model/paged-data';
import {CorporateEmployee} from '../model/corporate-employee';
import {Page} from '../model/page';
import { PushService } from './push.service'
// const companyData = require('../../assets/data/company.json');

@Injectable()
export class MockServerResultsService {
    companyData: any
    constructor(
        private _pushservice: PushService
    ) {
        this.companyData = this._pushservice.service
    }

    public getResults(page: Page): Observable<PagedData<CorporateEmployee>> {
        return Observable.of(this.companyData).map(data => this.getPagedData(page));
    }

    private getPagedData(page: Page): PagedData<CorporateEmployee> {
        const pagedData = new PagedData<CorporateEmployee>();
        page.totalElements = this.companyData.length;
        page.totalPages = page.totalElements / page.size;
        const start = page.pageNumber * page.size;
        const end = Math.min((start + page.size), page.totalElements);
        for (let i = start; i < end; i++) {
            const jsonObj = this.companyData[i];
            const employee = new CorporateEmployee(jsonObj.name, jsonObj.gender, jsonObj.company, jsonObj.age);
            pagedData.data.push(employee);
        }
        pagedData.page = page;
        return pagedData;
    }

}
